<!-- 优惠券 -->
<template>
  <el-main style="padding: 20px;">
	  <el-button type="primary" size="mini" icon="el-icon-plus" @click="setAdd">
	    添加
	  </el-button>

    <!-- 添加 -->
    <el-dialog
      :visible.sync="tianjia"
      width="80%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
                v-model="form.begin"
                type="datetime"
                placeholder="选择日期时间"
                default-time="12:00:00">
              </el-date-picker>
        </el-form-item>
         <el-form-item label="失效时间">
            <el-date-picker
                  v-model="form.end"
                  type="datetime"
                  placeholder="选择日期时间"
                  default-time="12:00:00">
                </el-date-picker>
          </el-form-item>
        <el-form-item label="减少金额">
          <el-input v-model="form.limit"></el-input>
        </el-form-item>
        <el-form-item label="满足价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
          <el-form-item label="发布数量">
          <el-input v-model="form.num"></el-input>
        </el-form-item>
          <el-button type="primary" @click="tianjiaQue">确定</el-button>
          <el-button @click="bianji=false">取消</el-button>
        <!-- </el-form-item> -->
      </el-form>
    </el-dialog>




    <el-dialog
      :visible.sync="bianji"
      width="80%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
                v-model="form.begin"
                type="datetime"
                placeholder="选择日期时间"
                default-time="12:00:00">
              </el-date-picker>
        </el-form-item>
         <el-form-item label="失效时间">
            <el-date-picker
                  v-model="form.end"
                  type="datetime"
                  placeholder="选择日期时间"
                  default-time="12:00:00">
                </el-date-picker>
          </el-form-item>
        <el-form-item label="减少金额">
          <el-input v-model="form.limit"></el-input>
        </el-form-item>
        <el-form-item label="满足价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="发布数量">
          <el-input v-model="form.num"></el-input>
        </el-form-item>
          <el-button type="primary" @click="bianjiQue">确定</el-button>
          <el-button @click="bianji=false">取消</el-button>
        <!-- </el-form-item> -->
      </el-form>
    </el-dialog>
    <div class="table-box">
      <el-table :data="data.arrays" style="width: 100%">
        <el-table-column prop="name" label="优惠券"></el-table-column>
    <el-table-column prop="begin" label="开始时间"></el-table-column>
		<el-table-column prop="end" label="结束时间"></el-table-column>
       <el-table-column label="操作">
         <template slot-scope="scope">
            <el-button @click="setbianji(scope.row)" type="text" size="mini">编辑</el-button>
            <el-button type="text" size="mini" @click="deleteObj(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
	  <el-pagination
	    background
	    layout="prev, pager, next"
	    :page-count="data.pages"
		:current-page="current"
		@current-change="chaxun"
		>
	  </el-pagination>
    </div>

  </el-main>
</template>

<script>
	export default{
	  data(){
	    return {
        tianjia:false,
	      data:[],
        list:[],
	      content:[],
        page:1,
        size:20,
        current:1,
        form:{},
        bianji:false
	    }
	  },
	  mounted: function () {
	    this.getData()
	  },
	  inject:['reload'],
	  methods: {
      setAdd(){
        this.tianjia=true;
        this.form={}
      },
      tianjiaQue(){
        let start1 = this.form.begin.toLocaleTimeString();
        let start2 = this.form.begin.toLocaleDateString();
        //console.log(start2.replace(/\//ig,'-') +' '+start1.substr(2))
        let end1 = this.form.end.toLocaleTimeString();
        let end2 = this.form.end.toLocaleDateString();
        this.form.begin = start2.replace(/\//ig,'-') +' '+start1.substr(2);
        this.form.end =end2.replace(/\//ig,'-') +' '+end1.substr(2);
        //console.log(this.form)
        this.postJson('/shop/coupon/save',this.form).then(res=>{
          //console.log(res)
          this.$message(res.data.msg);
          this.reload()
        })
      },
      bianjiQue(){
        //console.log(typeof(this.form.begin))
        if(typeof(this.form.begin)==Object){
          let start1 = this.form.begin.toLocaleTimeString();
          let start2 = this.form.begin.toLocaleDateString();
          this.form.begin = start2.replace(/\//ig,'-') +' '+start1.substr(2);
        }
        if(typeof(this.form.end)==Object){
          let end1 = this.form.end.toLocaleTimeString();
          let end2 = this.form.end.toLocaleDateString();
          this.form.end =end2.replace(/\//ig,'-') +' '+end1.substr(2);
        }
        let params = {}
        //console.log(params)
        this.postJson('/shop/coupon/update',this.form).then(res=>{
          //console.log(res)
          this.$message(res.data.msg);
          this.reload()
        })
      },
      setbianji(e){
        this.form = e;
        //console.log(this.form)
        this.bianji = true;
      },
		  deleteObj(val){
			  //console.log(val)
			  this.postRequest(`/shop/coupon/delete?ids=`+val.id).then(res => {
			     //console.log(res)
            this.$message(res.data.msg);
			     this.reload()
			  });
		  },
		  chaxun(val){
			  //console.log(val)
			  this.page = val;
			  this.getData()
		  },
		  getData(){  //查询优惠券
		  let that = this;
				that.postRequest(`/shop/coupon/list`,{pageNo:that.page,pageSize:that.size}).then(res => {
          //console.log(res)
				  that.data = res.data.data;
          this.list = res.data.data.arrays; 
          //console.log(this.list)
				});
		  }


	  }
	}
</script>

<style>
</style>
